﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PTCTTCNBK.Models;
using WebMatrix.WebData;
using System.Web.Security;

namespace PTCTTCNBK.Controllers
{  
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        #region variable
        public string KEY_LOGIN = "UserName";
        public string KEY_PASSWORD = "UserPassword";
        public string USER_ID = "UserID";
        public string FLAG_CHECK = "CheckUser";
        public string FLAG_CHECKLOGIN = "CheckLogin";
        #endregion

        public ActionResult Login()
        {
            //var dataContext = new PetroDataContext();
            //LoginUsers itemResult = new LoginUsers();
            //itemResult.User = dataContext.PtcTbl_Users.ToList();
            if (Session[FLAG_CHECKLOGIN] != null && (bool)Session[FLAG_CHECKLOGIN] == true)
                return RedirectToAction("Index", "Home");

            if (Session[FLAG_CHECK] != null)
            {    
               

                if ((bool)Session[FLAG_CHECK] == true)
                {
                    ViewBag.Alert = "Tài khoản hay mật khẩu nhập sai, vui lòng kiểm tra lại!";
                    ViewBag.Reset = "Nhấn vào đây để cài đặt lại";
                }
                else {
                    ViewBag.Alert = "";
                    ViewBag.Reset = "";
                }
            }
            return View();
        }
        public ActionResult BeforeLogin(string login, string password)
        {
            var dataContext = new PetroDataContext();
            LoginUsers itemResult = new LoginUsers();
            itemResult.User = dataContext.PtcTbl_Users.ToList();
            itemResult.Flag=false;
            //Session[USER_ID] = 0;
            Session[KEY_LOGIN] = login;
            Session[KEY_PASSWORD] = password;
            foreach (var item in itemResult.User)
            {
                if ((string)Session[KEY_LOGIN] == item.UserName && (string)Session[KEY_PASSWORD]==item.Password) {
                    Session[USER_ID] = item.ID;
                    UserLogin.Name = item.UserName;
                    Session["checkLogin"] = true;
                    Session[FLAG_CHECKLOGIN] = true;
                    return RedirectToAction("AfterLogin");
                }
            }
            itemResult.Flag = true;
            Session[FLAG_CHECK] = itemResult.Flag;
            return RedirectToAction("Login");
        }
        public ActionResult AfterLogin()
        {
            var dataContext = new PetroDataContext();
            AuthorityClass itemResult = new AuthorityClass();
            itemResult.Authority = dataContext.PtcView_Authorities.ToList();
            //Check permission
            foreach (var item in itemResult.Authority)
            {
                if (item.ID == (int)Session[USER_ID])
                {
                    if (item.AuthorityName == "ReportController")
                    {
                        Session["ReportController"] = true;
                    }
                    else if (item.AuthorityName == "ManageTankController")
                    {
                        Session["ManageTankController"] = true;
                    }
                    else if (item.AuthorityName == "FunctionImportController")
                    {
                        Session["FunctionImportController"] = true;
                    }
                    else if (item.AuthorityName == "ProductManagerController")
                    {
                        Session["ProductManagerController"] = true;
                    }
                    else if (item.AuthorityName == "ManageBillInOutController")
                    {
                        Session["ManageBillInOutController"] = true;
                    }
                    else if (item.AuthorityName == "FunctionCheckInController")
                    {
                        Session["FunctionCheckInController"] = true;
                    }
                    else if (item.AuthorityName == "FunctionOrderController")
                    {
                        Session["FunctionOrderController"] = true;
                    }
                    else if (item.AuthorityName == "ManageEmployeeController")
                    {
                        Session["ManageEmployeeController"] = true;
                    }
                    else if (item.AuthorityName == "ManageCustomerController")
                    {
                        Session["ManageCustomerController"] = true;
                    }
                    else if (item.AuthorityName == "AuthorityUsersController")
                    {
                        Session["AuthorityUsersController"] = true;
                    }
                    else if (item.AuthorityName == "AuthorityGroupsController")
                    {
                        Session["AuthorityGroupsController"] = true;
                    }
                }
            }
            return RedirectToAction("Index", "Home");
        }
    
        public ActionResult LogOff()
        {
            Session.Clear();
            Session.Abandon();
            UserLogin.Name = null;
            return RedirectToAction("Login", "Login");
        }



    }
 
    public class LoginUsers
    {
        public List<PtcTbl_User> User { get; set; }
        public bool Flag { get; set; }

    }
    public class AuthorityClass {
        public List<PtcView_Authority> Authority { get; set; } 
    }
}
